<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="110px"
    >
      <el-form-item label="订单号" prop="orderNumber">
        <el-input
          v-model="queryParams.orderNumber"
          placeholder="请输入订单号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item label="客户名称" prop="customerName">
        <el-input
          v-model="queryParams.customerName"
          placeholder="请输入客户名称"
          clearable
          @keyup.enter="handleQuery"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item label="订单类型" prop="dictOrderType">
        <el-select
          v-model="queryParams.dictOrderType"
          placeholder="请选择订单类型"
          clearable
          class="!w-220px"
        >
          <el-option
            v-for="(dict, index) in getIntDictOptions(DICT_TYPE.MARKET_ORDER_TYPE)"
            :key="index"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="产品编号" prop="productNumber">
        <el-input
          v-model="queryParams.productNumber"
          placeholder="请输入产品编号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item label="产品名称" prop="productName">
        <el-input
          v-model="queryParams.productName"
          placeholder="请输入产品名称"
          clearable
          @keyup.enter="handleQuery"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item label="产品规格" prop="productStandard">
        <el-input
          v-model="queryParams.productStandard"
          placeholder="请输入产品规格"
          clearable
          @keyup.enter="handleQuery"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item label="订单日期" prop="orderDate">
        <el-date-picker
          v-model="queryParams.orderDate"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item label="发货日期" prop="deliveryDate">
        <el-date-picker
          v-model="queryParams.deliveryDate"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item label="变更类型" prop="dictChangeType">
        <el-select
          v-model="queryParams.dictChangeType"
          placeholder="请选择变更类型"
          clearable
          class="!w-220px"
        >
          <el-option
            v-for="(dict, index) in getIntDictOptions(DICT_TYPE.CHANGE_TYPE)"
            :key="index"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="setSearchParams(queryParams)">搜索</el-button>
        <el-button @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <div class="operateClick">
      <el-button
        type="primary"
        :loading="exportLoading"
        v-hasPermi="['order:project-task-book:export']"
        @click="handleExport"
      >
        数据导出
      </el-button>

      <el-button
        type="primary"
        v-hasPermi="['order:project-task-book:headerSettings']"
        @click="openTableTitleSet"
      >
        表头设置
      </el-button>
    </div>
    <Table
      :columns="tableTitleList"
      :selection="tableObject.tableList.length ? true : false"
      @register="register"
      :data="tableObject.tableList"
      :loading="tableObject.loading"
      :pagination="{
        total: tableObject.total
      }"
      v-model:pageSize="tableObject.pageSize"
      v-model:currentPage="tableObject.currentPage"
      :tableHeight="tableHeight"
    >
      <template #action="{ row }">
        <el-button
          link
          type="primary"
          v-hasPermi="['order:project-task-book:details']"
          @click="detailsView(row.id)"
          >查看</el-button
        >
      </template>
    </Table>
  </ContentWrap>
  <!-- 表头设置 -->
  <table-width-set ref="tableTitleSet" @refresh="getLabelAll" />
  <!-- 导出 -->
  <ExportFile ref="exportRef" @download="downloadFun" />
</template>

<script setup lang="ts" name="ProjectTaskBook">
import { DICT_TYPE, getIntDictOptions } from '@/utils/dict'
import download from '@/utils/download'
import * as ProjectTaskBookApi from '@/api/orderManagement/projectTaskBook'
const { push } = useRouter()
let tableTitleList = ref([]) // 表头的数据

const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  orderNumber: null,
  dictOrderType: undefined,
  customerName: null,
  productNumber: null,
  productStandard: null,
  orderDate: undefined,
  deliveryDate: undefined,
  dictChangeType: undefined,
  productName: null
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中
// tableObject：表格的属性对象，可获得分页大小、条数等属性
// tableMethods：表格的操作对象，可进行获得分页、删除记录等操作getUserLabelPage
const { tableObject, tableMethods } = useTable({
  getListApi: ProjectTaskBookApi.getProjectTaskBookPage, // 分页接口
  defaultParams: queryParams
})
/** 查询列表 */
const getLabelAll = async () => {
  try {
    const data = await ProjectTaskBookApi.getProjectTaskBookPage(queryParams)
    let json = {
      align: 'left',
      label: '操作',
      field: 'action',
      isDetail: false,
      width: 80
    }
    data.labels.push(json)
    const { allSchemas } = useCrudSchemas(data.labels)
    tableTitleList.value = allSchemas.tableColumns as any
  } finally {
  }
}
// 获得表格的各种操作
const { getList, setSearchParams } = tableMethods

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/**打开表格表头设置**/
const tableTitleSet = ref()
const openTableTitleSet = () => {
  let code = 'projectTaskBook'
  tableTitleSet.value.open(tableTitleList.value, code)
}

/**查看**/
const detailsView = (id: number) => {
  push('/orderManagement/orderManagement/project/bookBetailsView?id=' + id)
}

/** 导出按钮操作 */
const exportRef = ref()
const handleExport = () => {
  exportRef.value.open()
}

/**表头内部方法 */
const usTableoperation = ref()
// 获取table组件
const register = (parent: any) => {
  usTableoperation.value = parent
}
// 导出数据
const downloadFun = async () => {
  const val = exportRef.value.params
  let params = {
    fileName: val.name,
    ids: ''
  }
  if (val.range === 'all') {
    params.ids = ''
  } else {
    params.ids = usTableoperation.value.selections.map((item) => (item as any).id).join(',')
  }
  ProjectTaskBookApi.exportProjectTaskBook(params).then((res) => {
    download.excel(res, val.name)
    clearSelectData()
  })
}

//清空多选
const clearSelectData = () => {
  usTableoperation.value.selections = []
  usTableoperation.value.clearTableSelect()
}

/** 初始化 **/
const tableHeight = ref(0)
onMounted(async () => {
  const queryParamsHeight = queryFormRef.value.$el.clientHeight + 2
  tableHeight.value = document.body.clientHeight - 132 - queryParamsHeight - 127
  getLabelAll()
  getList()
})
</script>
<style lang="scss" scoped>
.operateClick {
  height: 50px;
  display: flex;
  justify-content: space-between;
}

:deep(.el-select .el-input) {
  width: 100%;
}
</style>
